﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAL.Helpers;
using DAL.Repositories;
using Model.Entities;
using Model.ViewModels;

namespace Web.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        public ActionResult Index()
        {
            var apartmentRep = new ApartmentRepository();
            var apartments = apartmentRep.ReadAll();

            return View(apartments);
        }

        //
        // GET: /Admin/FilterCode
        public ActionResult FilterCode(string filterCode)
        {
            var apartmentRep = new ApartmentRepository();
            var apartments = apartmentRep.ReadByCode(filterCode) ?? new List<ApartmentListAdminVm>();
            return View("Index", apartments);
        }

        //
        // GET: /Admin/FilterModel
        public ActionResult FilterModel(string filterModel)
        {
            var apartmentRep = new ApartmentRepository();
            IList<ApartmentListAdminVm> apartments;

            if (filterModel != String.Empty)
            {
                apartments = apartmentRep.ReadByModel(filterModel) ?? new List<ApartmentListAdminVm>();
            }
            else
            {
                apartments = new List<ApartmentListAdminVm>();
            }

            return View("Index", apartments);
        }

        //
        // GET: /Admin/NewApartment/
        public ActionResult NewApartment()
        {
            return View();
        }

        //
        // POST: /Admin/NewApartment/
        [HttpPost]
        public ActionResult NewApartment(ApartmentCrudAdminVm model)
        {
            if (ModelState.IsValid)
            {
                var apartment = new Apartment()
                {
                    Code = model.Code,
                    Building = model.Building,
                    State = ApartmentState.Stan_za_prodaju,
                    Floor = model.Floor,
                    NumOfRooms = model.NumOfRooms,
                    Facade = model.Facade,
                    Model = (int)model.ApartmentModel,
                    Price = (double)model.Price,
                    Area = (double)model.Area,
                    HasTwoFloors = model.HasTwoFloors
                };
                apartment.TwoFloorsCode = model.HasTwoFloors ? model.Code : null;

                var indexTable = new ApartmentSearch()
                {
                    Code = model.Code,
                    Floor = EnumHelper.GetEnumName(typeof(Floor), model.Floor),
                    NumOfRooms = EnumHelper.GetEnumName(typeof(NumOfRooms), model.NumOfRooms),
                    Facade = EnumHelper.GetEnumName(typeof(ApartmentFacade), model.Facade),
                    Apartment = apartment
                };

                apartment.IndexTable = indexTable;
                var apartmentRep = new ApartmentRepository();
                apartmentRep.Create(apartment);

                return RedirectToAction("Index");
            }

            return View(model);
        }

        // GET: /Admin/DeleteApartment/5
        public ActionResult DeleteApartment(int id)
        {
            return View();
        }

        // brisanje članka
        // POST: /Admin/DeleteApartment/5
        [HttpPost]
        public ActionResult DeleteApartment(int id, FormCollection collection)
        {
            var apartmentRep = new ApartmentRepository();
            apartmentRep.Delete(id);

            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/EditApartment/5
        public ActionResult EditApartment(int id)
        {
            var apartmentRep = new ApartmentRepository();
            var apartment = apartmentRep.Read(id);

            var viewModel = new ApartmentCrudAdminVm()
            {
                Code = apartment.Code,
                Building = apartment.Building,
                State = apartment.State,
                Floor = apartment.Floor,
                NumOfRooms = apartment.NumOfRooms,
                Facade = apartment.Facade,
                ApartmentModel = apartment.Model,
                Price = apartment.Price,
                Area = apartment.Area,
                HasTwoFloors = apartment.HasTwoFloors
            };

            return View(viewModel);
        }

        //
        // POST: /Admin/EditApartment/5
        [HttpPost]
        public ActionResult EditApartment(int id, ApartmentCrudAdminVm model)
        {
            var apartmentRep = new ApartmentRepository();
            var apartment = apartmentRep.Read(id);

            if (ModelState.IsValid)
            {
                apartment.Code = model.Code;
                apartment.Building = model.Building;
                apartment.State = model.State;
                apartment.Floor = model.Floor;
                apartment.NumOfRooms = model.NumOfRooms;
                apartment.Facade = model.Facade;
                apartment.Model = (int)model.ApartmentModel;
                apartment.Price = (double)model.Price;
                apartment.Area = (double)model.Area;
                apartment.HasTwoFloors = model.HasTwoFloors;
                apartment.TwoFloorsCode = model.HasTwoFloors ? model.Code : null;

                apartmentRep.Update(apartment);

                return RedirectToAction("Index");
            }

            return View(model);
        }

        //
        // GET: /Admin/Rooms/Id
        public ActionResult Rooms(int id)
        {
            #region list of rooms

            var roomRep = new RoomRepository();
            var rooms = roomRep.ReadAll(id);
            var roomViewModel = new RoomCrudAdminVm() { Rooms = rooms };

            #endregion

            ViewData["ApartmentId"] = id;
            return View(roomViewModel);
        }

        //
        // POST: /Admin/Rooms/Id
        [HttpPost]
        public ActionResult Rooms(int id, RoomCrudAdminVm model)
        {
            if (ModelState.IsValid)
            {
                var room = new Room()
                {
                    RoomGroup = (int)model.RoomGroup,
                    Mark = model.Mark,
                    Type = model.Type,
                    Name = model.Name,
                    AreaValue = (double)model.Area
                };

                var roomRep = new RoomRepository();
                roomRep.Create(id, room);

                return RedirectToAction("Rooms", new { id = id });
            }

            ViewData["ApartmentId"] = id;
            return View(model);
        }

        //
        // GET: /Admin/EditRoom/5/6
        public ActionResult EditRoom(int id, int roomsNumberId)
        {
            var roomRep = new RoomRepository();
            var room = roomRep.Read(roomsNumberId);

            var viewModel = new RoomCrudAdminVm()
            {
                RoomGroup = room.RoomGroup,
                Mark = room.Mark,
                Type = room.Type,
                Name = room.Name,
                Area = room.AreaValue
            };

            ViewData["ApartmentId"] = id;
            return View(viewModel);
        }

        //
        // POST: /Admin/EditRoom/5/6
        [HttpPost]
        public ActionResult EditRoom(int id, int roomsNumberId, RoomCrudAdminVm model)
        {
            if (ModelState.IsValid)
            {
                var roomRep = new RoomRepository();
                model.Id = roomsNumberId;
                roomRep.Update(id, model);

                return RedirectToAction("Rooms", new { id = id });
            }
            
            return View(model);
        }

        // GET: /Admin/DeleteRoom/5/6
        public ActionResult DeleteRoom(int id, int roomsNumberId)
        {
            ViewData["ApartmentId"] = id;
            return View();
        }

        // POST: /Admin/DeleteRoom/5/6
        [HttpPost]
        public ActionResult DeleteRoom(int id, int roomsNumberId, FormCollection collection)
        {
            var roomRep = new RoomRepository();
            roomRep.Delete(id, roomsNumberId);

            return RedirectToAction("Rooms", new { id = id });
        }

        // GET: /Admin/GenerateRoom/5/6
        public ActionResult GenerateRoom(int id)
        {
            return View();
        }

        // POST: /Admin/GenerateRoom/5/6
        [HttpPost]
        public ActionResult GenerateRoom(int id, FormCollection collection)
        {
            var apartmentRep = new RoomRepository();
            apartmentRep.GenerateRoomsByModel(id);

            return RedirectToAction("Index");
        }
    }
}
